﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SSParallelAction : SSAction {
    private HashSet<SSAction> parallel;
    private HashSet<SSAction> waitingDelete;

    public static SSParallelAction GetSSAction() {
        SSParallelAction action = ScriptableObject.CreateInstance<SSParallelAction>();
        action.parallel = new HashSet<SSAction>();
        action.waitingDelete = new HashSet<SSAction>();
        return action;
    }

    public void Init(SSAction[] sequence, int identifier = 0, ISSActionCallback callback = null) {
        base.Init(identifier, callback);
        foreach (SSAction ac in sequence) {
            this.parallel.Add(ac);
        }
    }

    public void Init(List<SSAction> sequence, int identifier = 0, ISSActionCallback callback = null) {
        Init(sequence.ToArray(), identifier, callback);
    }

    public override void Update() {
        if (parallel.Count == 0) {
            SetOver(true);
            SendCompleteEvent();
        }
        foreach (SSAction ac in parallel) {
            ac.Update();
            if (ac.IsOver()) {
                waitingDelete.Add(ac);
            }
        }
        foreach (SSAction ac in waitingDelete) {
            parallel.Remove(ac);
        }
    }
}
